
package com.caration.shell.preference;

import android.content.Context;
import android.content.DialogInterface;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

import com.caration.sdk.utilities.DataUtils;
import com.caration.shell.R;
import com.caration.shell.config.CommonConfig;
import com.caration.shell.cr3d.SharedData;

public final class SettingFolderPreference implements OnPreferenceClickListener {
    public static final int TYPE_QUICK = 0;
    public static final int TYPE_NORMAL = 1;
    private static int sLastType = 0;
    private static int sType = 0;
    private static OnPreferenceClickListener sListener;
    private static PreferenceActivity sActivity;

    public final boolean onPreferenceClick(Preference preference) {
        if (CommonConfig.KEY_FOLDER_ANIM.equals(preference.getKey())) {
            show(getParent(), preference);
        } else if (CommonConfig.KEY_FOLDER_AUTO_CLOSE.equals(preference.getKey()))
            CommonConfig.setFolderAutoClose(((CheckBoxPreference) preference).isChecked());
        return true;
    }

    public static int validate(int i) {
        if (i >= 0) {
            if (i > 1)
                i = 1;
        } else {
            i = 0;
        }
        return i;
    }

    public static void onDestory() {
        sActivity = null;
        sListener = null;
    }

    static void show(Context context, final Preference preference) {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
        builder.setTitle(R.string.mn_folder_animation);
        sLastType = sType = CommonConfig.getFolderAnimation();
        String as[] = new String[2];
        as[0] = DataUtils.getString(SharedData.getContext(),R.string.mn_folder_animation_quick);
        as[1] = DataUtils.getString(SharedData.getContext(),R.string.mn_folder_animation_normal);
        builder.setSingleChoiceItems(as, sLastType, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                setType(which);
            }
        });
        builder.setPositiveButton(R.string.public_action_ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (getLastType() != getType()) {
                    CommonConfig.setFolderAnimation(getType());
                    setSummary(preference);
                }
            }
        });
        builder.setNegativeButton(R.string.public_action_cancel, null);
        builder.create().show();
    }

    public static void setSummary(Preference preference) {
        switch (CommonConfig.getFolderAnimation()) {
            case TYPE_QUICK:
                preference.setSummary(DataUtils.getString(SharedData.getContext(),R.string.mn_folder_animation_quick));
                break;

            case TYPE_NORMAL:
                preference.setSummary(DataUtils.getString(SharedData.getContext(),R.string.mn_folder_animation_normal));
                break;
        }
    }

    public static void init(PreferenceActivity activity) {
        sActivity = activity;
        if (sListener == null)
            sListener = new SettingFolderPreference();

        Preference preference = activity.findPreference(CommonConfig.KEY_FOLDER_ANIM);
        preference.setOnPreferenceClickListener(sListener);
        setSummary(preference);

        CheckBoxPreference checkPref = (CheckBoxPreference) activity
                .findPreference(CommonConfig.KEY_FOLDER_AUTO_CLOSE);
        checkPref.setOnPreferenceClickListener(sListener);
        checkPref.setChecked(CommonConfig.isFolderAutoClose());
    }

    static PreferenceActivity getParent() {
        return sActivity;
    }

    static void setType(int type) {
        sType = type;
    }

    static int getLastType() {
        return sLastType;
    }

    static int getType() {
        return sType;
    }
}
